<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Devices and Channels</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="kvaser.gif"/></td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('page_user_guide_device_and_channel.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">Devices and Channels </div>  </div>
</div><!--header-->
<div class="contents">
<div class="toc"><h3>Table of Contents</h3>
<ul><li class="level1"><a href="#section_user_guide_identifying_devices">Identifying Devices and Channels</a></li>
<li class="level1"><a href="#section_user_guide_unique_device">Channel Information</a></li>
<li class="level1"><a href="#section_user_guide_cust_channel_name">Customized Channel Name</a></li>
<li class="level1"><a href="#section_user_guide_virtual">Virtual Channels</a></li>
</ul>
</div>
<div class="textblock"><h1><a class="anchor" id="section_user_guide_identifying_devices"></a>
Identifying Devices and Channels</h1>
<p>Once we have called <a class="el" href="group__can__general.html#gaff1ec1d3416d3bdd56336a7b9ac008b1">canInitializeLibrary()</a> to enumerate the connected Kvaser CAN devices, we can call <a class="el" href="group__can__general.html#ga65169ca633cd30aa92b8a80e28a5378b">canGetNumberOfChannels()</a> to get the number of enumerated channels in our system.</p>
<p>We can then call <a class="el" href="group__can__general.html#gab9552d1a588b0dbc144b097acba017b2">canGetChannelData()</a> for the CANlib channel numbers 0, 1, 2,…, n-1 (where n is the number returned by <a class="el" href="group__can__general.html#ga65169ca633cd30aa92b8a80e28a5378b">canGetNumberOfChannels()</a>) to get information about that specific channel.</p>
<p><b>Example</b>. This code snippet prints the number of enumerated channels found in the PC.</p>
<div class="fragment"><div class="line">stat = <a class="code" href="group__can__general.html#ga65169ca633cd30aa92b8a80e28a5378b">canGetNumberOfChannels</a>(&amp;chanCount);</div><div class="line"><span class="keywordflow">if</span> (stat &lt; 0) ... ;</div><div class="line"><span class="keywordflow">if</span> (chanCount &lt; 0 || chanCount &gt; 300) {</div><div class="line">    printf(<span class="stringliteral">&quot;ChannelCount = %d but I don&#39;t believe it.\n&quot;</span>, chanCount);</div><div class="line">    exit(1); <span class="comment">// Error</span></div><div class="line">} <span class="keywordflow">else</span> {</div><div class="line">    printf(<span class="stringliteral">&quot;%d channels.\n&quot;</span>, chanCount);</div><div class="line">}</div></div><!-- fragment --><h1><a class="anchor" id="section_user_guide_unique_device"></a>
Channel Information</h1>
<p>Use <a class="el" href="group__can__general.html#gab9552d1a588b0dbc144b097acba017b2">canGetChannelData()</a> to obtain data for a specific channel, for example, the hardware type of the CAN interface. The same information can also be retrieved using <a class="el" href="group___c_a_n.html#ga909e5c14b09fe76bad280475138d503e">canGetHandleData()</a>, which instead takes an active <a class="el" href="canlib_8h.html#a75f2e3a4c7b1e47c8c6b25b76975b510">CanHandle</a>.</p>
<p>To uniquely identify a device, we need to look at both the <a class="el" href="canlib_8h.html#a9062beaa59d9787667bac6cbbc6b4e20">canCHANNELDATA_CARD_UPC_NO</a> and <a class="el" href="canlib_8h.html#a8b3b4903a766565c95afd7c90f76d994">canCHANNELDATA_CARD_SERIAL_NO</a>.</p>
<p>Use <a class="el" href="group__can__general.html#gaeaa24db97af22478ca51d48636c7bb12">canIoCtl()</a> to obtain various pieces of information for a specific handle, or to perform special operations.</p>
<p><b>Example</b>. This code snippet loops through all known channels and prints the type of the CAN card they're on.</p>
<div class="fragment"><div class="line">stat = <a class="code" href="group__can__general.html#ga65169ca633cd30aa92b8a80e28a5378b">canGetNumberOfChannels</a>(&amp;chanCount);</div><div class="line"><span class="keywordflow">if</span> (stat &lt; 0) ... ;</div><div class="line"><span class="keywordflow">for</span> (i=0; i &lt; chanCount; i++) {</div><div class="line">    <a class="code" href="linlib_8h.html#a798af1e30bc65f319c1a246cecf59e39">DWORD</a> tmp;</div><div class="line"></div><div class="line">    printf(<span class="stringliteral">&quot;== Channel %d ===============================\n&quot;</span>, i);</div><div class="line"></div><div class="line">    stat = <a class="code" href="group__can__general.html#gab9552d1a588b0dbc144b097acba017b2">canGetChannelData</a>(i, <a class="code" href="canlib_8h.html#ace40b923db9c9dc4f3f47ab1032a3db2">canCHANNELDATA_CARD_TYPE</a>, &amp;tmp, <span class="keyword">sizeof</span>(tmp));</div><div class="line">    <span class="keywordflow">if</span> (stat &lt; 0) ...; <span class="comment">// Error</span></div><div class="line">    printf(<span class="stringliteral">&quot;cardtype =              0x%08lx\n&quot;</span>, tmp);</div><div class="line">}</div></div><!-- fragment --><p>See <a class="el" href="how-to_2c_2list_channels_8c-example.html">how-to/c/listChannels.c</a> for code sample.</p>
<h1><a class="anchor" id="section_user_guide_cust_channel_name"></a>
Customized Channel Name</h1>
<p>It is possible to set the customized name returned by <a class="el" href="canlib_8h.html#a0e91dd0d999b3043c6da5cb911dd83d8">canCHANNELDATA_CUST_CHANNEL_NAME</a> on the device using Kvaser Device Guide by right clicking on the device channel and selecting "Edit Channel Name"</p>
<div class="image">
<img src="edit_channel_name.png" alt="edit_channel_name.png"/>
<div class="caption">
Setting the device's Channel Name from inside Kvaser Device Guide</div></div>
<p> Note that if no channel name is set, <a class="el" href="canstat_8h.html#a52b5e5c71832b0bd3c6a5b1fd48583e7a41e892edb8f1b6f9f609599df8e20bdf">canERR_NOT_IMPLEMENTED</a> will be returned by <a class="el" href="group__can__general.html#gab9552d1a588b0dbc144b097acba017b2">canGetChannelData()</a> when accessing <a class="el" href="canlib_8h.html#a0e91dd0d999b3043c6da5cb911dd83d8">canCHANNELDATA_CUST_CHANNEL_NAME</a>.</p>
<h1><a class="anchor" id="section_user_guide_virtual"></a>
Virtual Channels</h1>
<p>CANlib supports virtual channels that you can use for development, test, or demonstration purposes when you don't have any hardware installed.</p>
<p>For installation information, see <a class="el" href="section_install_linux.html#section_install_virtual_device_linux">Installing Virtual Devices</a> on Linux and <a class="el" href="section_install_windows.html#section_install_virtual_device_windows">Installing Additional Virtual Devices</a> for Windows.</p>
<p>There are 32 virtual busses on each virtual device, and you can connect any virtual channel to any virtual bus on the same virtual device. The default connection of a virtual device is that all of it's channels are connected to the same virtual bus.</p>
<dl class="section note"><dt>Note</dt><dd>The virtual busses are not shared between the virtual devices.</dd></dl>
<p>We can use <a class="el" href="group__can__general.html#gaeaa24db97af22478ca51d48636c7bb12">canIoCtl()</a> with <a class="el" href="canlib_8h.html#a4d08b72dd42b68dbac4c3367ff5dc749">canIOCTL_DISCONNECT_FROM_VIRTUAL_BUS</a> to disconnect a virtual channel from a virtual bus. Similarly, <a class="el" href="canlib_8h.html#a313cdb4388e60d7ff769313694a153de">canIOCTL_CONNECT_TO_VIRTUAL_BUS</a> can be used to connect the now disconnected virtual channel to a new virtual bus.</p>
<p>To open a virtual channel, call <a class="el" href="group___c_a_n.html#gac377d182232fb4ec2fed881c2b9ab300">canOpenChannel()</a> with the appropriate channel number, and specify <a class="el" href="canlib_8h.html#a8e4ead14e67e49e27674c706e0d88f23">canOPEN_ACCEPT_VIRTUAL</a> in the flags argument to <a class="el" href="group___c_a_n.html#gac377d182232fb4ec2fed881c2b9ab300">canOpenChannel()</a>.</p>
<p><b>Example</b>. This code snippet disconnects the first virtual channel from the virtual bus 0, and reconnects it to virtual bus 1.</p>
<div class="fragment"><div class="line"><a class="code" href="canlib_8h.html#a75f2e3a4c7b1e47c8c6b25b76975b510">CanHandle</a> hnd;</div><div class="line"></div><div class="line">hnd = <a class="code" href="group___c_a_n.html#gac377d182232fb4ec2fed881c2b9ab300">canOpenChannel</a>(0, <a class="code" href="canlib_8h.html#a8e4ead14e67e49e27674c706e0d88f23">canOPEN_ACCEPT_VIRTUAL</a>);</div><div class="line"><span class="keywordflow">if</span> (hnd &lt; 0) {</div><div class="line">    printf(<span class="stringliteral">&quot;Open failed, stat=%d\n&quot;</span>, hnd);</div><div class="line">}</div><div class="line"><span class="comment">// Disconnect from virtual bus 0</span></div><div class="line"><a class="code" href="group__can__general.html#gaeaa24db97af22478ca51d48636c7bb12">canIoCtl</a>(hnd, <a class="code" href="canlib_8h.html#a4d08b72dd42b68dbac4c3367ff5dc749">canIOCTL_DISCONNECT_FROM_VIRTUAL_BUS</a>, 0)</div><div class="line"></div><div class="line"><span class="comment">// Connect to virtual bus 1</span></div><div class="line"><a class="code" href="group__can__general.html#gaeaa24db97af22478ca51d48636c7bb12">canIoCtl</a>(hnd, canIOCTL_CONNECT_TO_VIRTUAL_BUS, 1)</div></div><!-- fragment --> </div></div><!-- contents -->
</div><!-- doc-content -->
<!-- HTML footer for doxygen 1.8.13-->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="navelem"><a class="el" href="page_canlib.html">CAN bus API (CANlib)</a></li>
    <li class="footer"> (canlib 5.38) Tue Feb 15 2022</li>
  </ul>
</div>
</body>
</html>
